﻿using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;

namespace WpfApp1
{
    public class CodesContext : ObservableObject
    {
        public ObservableCollection<CodeNode> CodesNotes { get; } = new ObservableCollection<CodeNode>();
        private CodeNode _selectedCode;

        public CodeNode SelectedCode
        {
            get => _selectedCode;
            set => Set(ref _selectedCode, value);
        }

        public CodesContext()
        {
            获取节点();
            SelectedCode = CodesNotes.FirstOrDefault();
        }

        private Dictionary<string, CodeNode> dic = new Dictionary<string, CodeNode>();

        private void 获取节点()
        {
            App.DB.Queryable<Code>()
                .OrderBy(x => x.关键字)
                .ForEach(code =>
            {
                CodeNode cn = new CodeNode(code);
                dic.Add(code.关键字, cn);
                if (dic.ContainsKey(cn.GetParent ?? ""))
                {
                    dic[cn.GetParent].Items.Add(cn);
                }
                else
                {
                    CodesNotes.Add(cn);
                }
            });
        }
    }
}